package com.wcl.hencodertest.test06_drawing;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ConvertUtils;

/**
 * @项目名： HenCoderTest
 * @包名： com.wcl.hencodertest
 * @文件名: PlateView
 * @创建者: kilin
 * @创建时间: 2020/4/15 17:30
 * @描述： TODO
 */
public class PlateView extends View {
    private static final String TAG = "PlateView";
    public static final int RADIUS = ConvertUtils.dp2px(100);

    public static final int ANGLE = 120;
    public static final int POINTER_LENGTH = ConvertUtils.dp2px(90);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    PathDashPathEffect mEffect;
    Path arc = new Path();
    Path dash = new Path();

    private float mValue;
    private ValueAnimator mValueAnimator;

    public PlateView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    {
        mValueAnimator = ValueAnimator.ofFloat(0, 1);
        mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                mValue = (float) valueAnimator.getAnimatedValue();
                Log.i(TAG, "onAnimationUpdate: "+mValue);
                invalidate();
            }
        });
        mValueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mValueAnimator.setRepeatMode(ValueAnimator.REVERSE);
        mValueAnimator.setDuration(4000);
        mValueAnimator.start();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(ConvertUtils.dp2px(2));


        arc.addArc(getWidth() / 2 - RADIUS, getHeight() / 2 - RADIUS, getWidth() / 2 + RADIUS, getHeight() / 2 + RADIUS, 90 + ANGLE / 2, 360 - ANGLE);


        dash.addRect(0, 0, ConvertUtils.dp2px(2), ConvertUtils.dp2px(5), Path.Direction.CW);
        PathMeasure pathMeasure = new PathMeasure(arc, false);

        mEffect = new PathDashPathEffect(dash, (pathMeasure.getLength() - ConvertUtils.dp2px(2)) / 20, 0, PathDashPathEffect.Style.ROTATE);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(Color.BLACK);
        canvas.drawArc(getWidth() / 2 - RADIUS, getHeight() / 2 - RADIUS, getWidth() / 2 + RADIUS,
                getHeight() / 2 + RADIUS, 90 + ANGLE / 2, 360 - ANGLE, false, paint);

        paint.setPathEffect(mEffect);
        canvas.drawArc(getWidth() / 2 - RADIUS, getHeight() / 2 - RADIUS, getWidth() / 2 + RADIUS,
                getHeight() / 2 + RADIUS, 90 + ANGLE / 2, 360 - ANGLE, false, paint);
        paint.setPathEffect(null);

        paint.setColor(Color.RED);
        canvas.drawLine(getWidth() / 2, getHeight() / 2,
                (float) (POINTER_LENGTH * Math.cos(Math.toRadians(150 + mValue * (360 - ANGLE))) + getWidth() / 2),
                (float) (POINTER_LENGTH * Math.sin(Math.toRadians(150 + mValue * (360 - ANGLE))) + getHeight() / 2), paint);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mValueAnimator != null) {
            mValueAnimator.cancel();
        }
    }
}
